package edu.pku.sei.transformation.atl.adapter;

import edu.pku.sei.transformation.atl.rule.IATLExpression;
import edu.pku.sei.transformation.editor.model.RuleInvoke;
import edu.pku.sei.transformation.editor.model.RuleInvokeParameter;
import edu.pku.sei.transformation.editor.model.TypedModel;
import edu.pku.sei.transformation.editor.model.variable.ObjectVariable;
import edu.pku.sei.transformation.editor.views.problem.ProblemView;

public abstract class ATLInvokeExpression implements IATLExpression {
	RuleInvoke ruleInvoke = null;
	TypedModel target = null;
	String input = null;
	String output = null;
	String outputName = null;

	public ATLInvokeExpression(RuleInvoke invoke, TypedModel target)
			throws Exception {
		this.ruleInvoke = invoke;
		this.target = target;
		// only support 2 parameters
		// one parameter for from pattern
		// the other for to pattern
		// actual parameter must be variable not expression
		if (ruleInvoke.getParameters().size() != 2) {
			ProblemView.addError(ruleInvoke.getModel(),
					ProblemView.CODEGEN_ATL, "invoke error",
					"only support invoke with 2 parameters", invoke.getCaller()
							.getName()
							+ "->" + invoke.getCallee().getName());
			throw new Exception("only support invoke with 2 parameters");
		}
		for (Object obj : ruleInvoke.getParameters()) {
			if (obj instanceof RuleInvokeParameter) {
				RuleInvokeParameter p = (RuleInvokeParameter) obj;
				if (p.getFormalParameter() instanceof ObjectVariable) {
					ObjectVariable ov = (ObjectVariable) p.getFormalParameter();
					if (ov.getPattern().getOwner().getTypedModel() == target) {
						output = p.getActualParameterString();
						outputName = ov.getName();
					} else {
						input = p.getActualParameterString();
					}
				} else {
					ProblemView.addError(ruleInvoke.getModel(),
							ProblemView.CODEGEN_ATL, "invoke error",
							"formal parameter must be ObjectVariable", invoke
									.getCaller().getName()
									+ "->" + invoke.getCallee().getName());
					throw new Exception(
							"formal parameter must be ObjectVariable");
				}
			} else {
				throw new Exception("invoke parameter type error");
			}
		}
	}

	@Override
	public String getContent() {
		return null;
	}

}
